﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using VPC.BO;
using VPC.BL;
using VPC.EX;

namespace VPC.UI
{
    public partial class WelcomeForm : Form
    {
        public WelcomeForm()
        {
            InitializeComponent();
            buttonEdit.Hide();
            buttonDelete.Hide();
        }

        private void WelcomeForm_Load(object sender, EventArgs e)
        {
            textBoxContactId.Focus();

            // In case of Advanced Search when the Contact Id comes from the Advanced Search Form this is used
            if (textBoxContactId.Text != "")
            {
                showData();
            }

        }

        //showData method is used to fetch Contact Details using Contact Id
        public void showData()
        {
            try
            {
                Int32 iContactId = Convert.ToInt32(textBoxContactId.Text);

                ContactBO objContactBO = new ContactBO();

                objContactBO = ContactBL.GetContactDetails(iContactId);

                if (objContactBO.ContactId != 0)
                {
                    labelContactIdResult.Text = objContactBO.ContactId.ToString();

                    string sFName                   = objContactBO.FirstName;
                    string sLName                   = objContactBO.LastName;
                    sFName                          = sFName.Trim();
                    sLName                          = sLName.Trim();
                    labelNameResult.Text            = sFName + " " + sLName;

                    labelFonctionResult.Text        = objContactBO.Fonction;
                    labelCompagnyResult.Text        = objContactBO.Compagny;
                    labelMobileNumber1Result.Text   = objContactBO.MobileNumber1;
                    labelMobileNumber2Result.Text   = objContactBO.MobileNumber2;
                    labelMobileNumber3Result.Text   = objContactBO.MobileNumber3;
                    labelHomeNumber1Result.Text     = objContactBO.HomeNumber1;
                    labelHomeNumber2Result.Text     = objContactBO.HomeNumber2;
                    labelHomeNumber3Result.Text     = objContactBO.HomeNumber3;
                    labelWorkNumber1Result.Text     = objContactBO.WorkNumber1;
                    labelWorkNumber2Result.Text     = objContactBO.WorkNumber2;
                    labelWorkNumber3Result.Text     = objContactBO.WorkNumber3;
                    labelFaxNumber1Result.Text      = objContactBO.FaxNumber1;
                    labelFaxNumber2Result.Text      = objContactBO.FaxNumber2;
                    labelFaxNumber3Result.Text      = objContactBO.FaxNumber3;
                    labelStreetResult.Text          = objContactBO.Street;
                    labelLocalityResult.Text        = objContactBO.Locality;
                    labelCodeResult.Text            = objContactBO.Code;
                    labelCountryResult.Text         = objContactBO.Country;
                    labelEmail1Result.Text          = objContactBO.Email1;
                    labelEmail2Result.Text          = objContactBO.Email2;
                    labelEmail3Result.Text          = objContactBO.Email3;

                    buttonEdit.Visible = true;
                    buttonDelete.Visible = true;
                    buttonExport.Visible = true;
                }

                else
                {
                    MessageBox.Show("Contact Id doesn't exist");
                    textBoxContactId.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid Contact Id");
            }

        }

        //Code for Searching a Contact
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            if (textBoxContactId.Text != "")
            {
                showData();
            }
            else if (textBoxFirstName.Text != "")
            {
                string sFName = textBoxFirstName.Text;

                List<ContactBO> lstContactDetails = new List<ContactBO>();
                lstContactDetails = ContactBL.GetContactDetails(sFName);

                if (lstContactDetails.Count != 0)
                {
                    AdvancedSearchForm frmAS = new AdvancedSearchForm();
                    frmAS.textBoxFirstName.Text = sFName;
                    this.Hide();
                    frmAS.AdvancedSearchForm_Load();
                    frmAS.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Contact list doesn't match those informations");
                }

            }
            else
            {
                MessageBox.Show("Enter a Contact Id or any informations");
            }
        }

        //Code for Add New Contact Menu Item
        private void manualInputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNewContact frmAddNew = new AddNewContact();
            frmAddNew.ShowDialog();
        }

        //Code for Logoff Menu Item
        private void logoffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoginForm frmLogin = new LoginForm();
            MessageBox.Show("You have Successfully Logged off");
            this.Hide();
            frmLogin.ShowDialog();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            Int32 iContactId = Convert.ToInt32(labelContactIdResult.Text);

            DialogResult result1 = MessageBox.Show("Are you sure you want to delete this Contact?",
                                  "Delete the Contact",
                                   MessageBoxButtons.YesNo);

            if (result1 == DialogResult.Yes)
            {
                int result = ContactBL.RemoveContact(iContactId);

                if (result > 0)
                {
                    MessageBox.Show("Contact Removed Successfully");
                    this.textBoxContactId.Text = "";
                    this.set_undefined();
                }
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditContact frmEditContact = new EditContact();
            frmEditContact.labelContactId.Text = labelContactIdResult.Text;
            frmEditContact.EditContact_Load();
            frmEditContact.ShowDialog();
            this.textBoxContactId.Text = this.labelContactIdResult.Text;
            this.showData();
        }

        private void set_undefined()
        {
            this.labelFonctionResult.Text = "Undefined";
            this.labelCompagnyResult.Text = "Undefined";
            this.labelNameResult.Text = "Undefined";
            this.labelContactIdResult.Text = "Undefined";
            this.labelFaxNumber3Result.Text = "Undefined";
            this.labelFaxNumber2Result.Text = "Undefined";
            this.labelFaxNumber1Result.Text = "Undefined";
            this.labelWorkNumber3Result.Text = "Undefined";
            this.labelWorkNumber2Result.Text = "Undefined";
            this.labelWorkNumber1Result.Text = "Undefined";
            this.labelHomeNumber3Result.Text = "Undefined";
            this.labelHomeNumber2Result.Text = "Undefined";
            this.labelHomeNumber1Result.Text = "Undefined";
            this.labelMobileNumber3Result.Text = "Undefined";
            this.labelMobileNumber2Result.Text = "Undefined";
            this.labelMobileNumber1Result.Text = "Undefined";
            this.labelEmail3Result.Text = "Undefined";
            this.labelEmail2Result.Text = "Undefined";
            this.labelEmail1Result.Text = "Undefined";
            this.labelCodeResult.Text = "Undefined";
            this.labelLocalityResult.Text = "Undefined";
            this.labelStreetResult.Text = "Undefined";
            this.labelCountryResult.Text = "Undefined";
        }

        private void viewAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ViewAllContacts frmViewAll = new ViewAllContacts();
            frmViewAll.ViewAllContacts_Load();
            frmViewAll.ShowDialog();
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "vCard (*.vcf)|*.vcf";
            saveFileDialog1.FilterIndex = 1;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(saveFileDialog1.FileName);//Do what you want here

                ContactEX jo = new ContactEX(saveFileDialog1.FileName);

                Int32 iContactId = Convert.ToInt32(textBoxContactId.Text);
                ContactBO objContactBO = new ContactBO();
                objContactBO = ContactBL.GetContactDetails(iContactId);

                jo._surname = objContactBO.FirstName;
                jo._given = objContactBO.LastName;
                jo._title = labelFonctionResult.Text;
                jo._org = labelCompagnyResult.Text;       
         
                if (labelEmail1Result.Text != "Undefined")
                    jo.addEmail(labelEmail1Result.Text);
                if (labelEmail2Result.Text != "Undefined")
                    jo.addEmail(labelEmail2Result.Text);
                if (labelEmail3Result.Text != "Undefined")
                    jo.addEmail(labelEmail3Result.Text);

                if (labelMobileNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.cell, labelMobileNumber1Result.Text);
                if (labelMobileNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.cell, labelMobileNumber2Result.Text);
                if (labelMobileNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.cell, labelMobileNumber3Result.Text);

                if (labelHomeNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.home, labelHomeNumber1Result.Text);
                if (labelHomeNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.home, labelHomeNumber2Result.Text);
                if (labelHomeNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.home, labelHomeNumber3Result.Text);

                if (labelWorkNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.work, labelWorkNumber1Result.Text);
                if (labelWorkNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.work, labelWorkNumber2Result.Text);
                if (labelWorkNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.work, labelWorkNumber3Result.Text);

                if (labelFaxNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.fax, labelFaxNumber1Result.Text);
                if (labelFaxNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.fax, labelFaxNumber2Result.Text);
                if (labelFaxNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.fax, labelFaxNumber3Result.Text);

                jo.addAddress(etype.work, labelStreetResult.Text, labelLocalityResult.Text, labelCodeResult.Text, labelCountryResult.Text);
                jo.export();

            } 
        }

        private void oCRInputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OcrForm frmocr = new OcrForm();
            frmocr.ShowDialog();
        }

        private void importVCFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "VCF files (*.vcf)|*.vcf";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (TextReader rdr = new StreamReader(openFileDialog1.FileName))
                {
                    string line;
                    
                    string LastName = "", FirstName = "";
                    string Company = "", Title = "";
                    string MobilePhone1 = "", MobilePhone2 = "", MobilePhone3 = "";
                    string HomePhone1 = "", HomePhone2 = "", HomePhone3 = "";
                    string WorkPhone1 = "", WorkPhone2 = "", WorkPhone3 = "";
                    string FaxPhone1 = "", FaxPhone2 = "", FaxPhone3 = "";
                    string Email1 = "", Email2 = "", Email3 = "";

                    while ((line = rdr.ReadLine()) != null)
                    {
                        Regex names = new Regex(@"^N:([\w]+|[\s]+);([\w]+|[\s]+);;;$");             // NOM + PRENOM
                        Regex company = new Regex(@"^ORG:([\w]+|[\s]+)$");                          // COMPANY
                        Regex title = new Regex(@"^TITLE:([\w]+|[\s]+)$");                          // TITLE
                        Regex mobilePhone = new Regex(@"^TEL;TYPE=cell,voice;VALUE=tel:([\d]+)$");  // MOBILE
                        Regex homePhone = new Regex(@"^TEL;TYPE=home,voice;VALUE=tel:([\d]+)$");    // HOME
                        Regex workPhone = new Regex(@"^TEL;TYPE=work,voice;VALUE=tel:([\d]+)$");    // WORK
                        Regex faxPhone = new Regex(@"^TEL;TYPE=fax,voice;VALUE=tel:([\d]+)$");      // FAX
                        Regex email = new Regex(@"^EMAIL:([\w\-.]+@[\w\-.]+)$");                    // EMAIL

                        # region if/else if/else
                        if (names.IsMatch(line))
                        {
                            Match m = names.Match(line);
                            LastName = m.Groups[2].Value;
                            FirstName = m.Groups[1].Value;
                        }
                        else if (company.IsMatch(line)) 
                        {
                            Match m = company.Match(line);
                            Company = m.Groups[1].Value;
                        }
                        else if (title.IsMatch(line))
                        {
                            Match m = title.Match(line);
                            Title = m.Groups[1].Value;
                        }
                        else if (mobilePhone.IsMatch(line))
                        {
                            Match m = mobilePhone.Match(line);
                            Boolean control = false;

                            if (MobilePhone3 == "")
                            {
                                if (MobilePhone2 == "")
                                {
                                    if (MobilePhone1 == "")
                                    {
                                        MobilePhone1 = m.Groups[1].Value;
                                        control = true;
                                    }
                                    if (control != true)
                                    {
                                        MobilePhone2 = m.Groups[1].Value;
                                        control = true;
                                    }
                                }
                                if (control != true)
                                    MobilePhone3 = m.Groups[1].Value;
                            }
                        }
                        else if (homePhone.IsMatch(line))
                        {
                            Match m = homePhone.Match(line);
                            Boolean control = false;

                            if (HomePhone3 == "")
                            {
                                if (HomePhone2 == "")
                                {
                                    if (HomePhone1 == "")
                                    {
                                        HomePhone1 = m.Groups[1].Value;
                                        control = true;
                                    }
                                    if (control != true)
                                    {
                                        HomePhone2 = m.Groups[1].Value;
                                        control = true;
                                    }
                                }
                                if (control != true)
                                    HomePhone3 = m.Groups[1].Value;
                            }
                        }
                        else if (workPhone.IsMatch(line))
                        {
                            Match m = workPhone.Match(line);
                            Boolean control = false;

                            if (WorkPhone3 == "")
                            {
                                if (WorkPhone2 == "")
                                {
                                    if (WorkPhone1 == "")
                                    {
                                        WorkPhone1 = m.Groups[1].Value;
                                        control = true;
                                    }
                                    if (control != true)
                                    {
                                        WorkPhone2 = m.Groups[1].Value;
                                        control = true;
                                    }
                                }
                                if (control != true)
                                    WorkPhone3 = m.Groups[1].Value;
                            }
                        }
                        else if (faxPhone.IsMatch(line))
                        {
                            Match m = faxPhone.Match(line);
                            Boolean control = false;

                            if (FaxPhone3 == "")
                            {
                                if (FaxPhone2 == "")
                                {
                                    if (FaxPhone1 == "")
                                    {
                                        FaxPhone1 = m.Groups[1].Value;
                                        control = true;
                                    }
                                    if (control != true)
                                    {
                                        FaxPhone2 = m.Groups[1].Value;
                                        control = true;
                                    }
                                }
                                if (control != true)
                                    FaxPhone3 = m.Groups[1].Value;
                            }
                        }
                        else if (email.IsMatch(line))
                        {
                            Match m = email.Match(line);
                            Boolean control = false;

                            if (Email3 == "")
                            {
                                if (Email2 == "")
                                {
                                    if (Email1 == "")
                                    {
                                        Email1 = m.Groups[1].Value;
                                        control = true;
                                    }
                                    if (control != true)
                                    {
                                        Email2 = m.Groups[1].Value;
                                        control = true;
                                    }
                                }
                                if (control != true)
                                    Email3 = m.Groups[1].Value;
                            }
                        }
                        else { }
                        #endregion 

                        //Regex adress = new Regex("^ADR;TYPE=work;LABEL=" + "\"" + @"([\w]+|[\d]+|[\s]+)" + "\\n" + @"([\w]+|[\d]+|[\s]+)" + "\\n" + @"([\w]+|[\d]+|[\s]+):;;([\w]+|[\d]+|[\s]+);([\w]+|[\d]+|[\s]+);;([\w]+|[\d]+|[\s]+);([\w]+|[\d]+|[\s]+)$");                 // NOM + PRENOM
                        //Boolean myBooladress = adress.IsMatch(line);
                    }

                    AddNewContact frmAddNew = new AddNewContact();

                    frmAddNew.firstNameTextfield.Text = FirstName;
                    frmAddNew.lastNameTextfield.Text = LastName;
                    frmAddNew.fonctionTextfield.Text = Title;
                    frmAddNew.compagnyTextfield.Text = Company;
                    frmAddNew.mobileNumber1Textfield.Text = MobilePhone1;
                    frmAddNew.mobileNumber2Textfield.Text = MobilePhone2;
                    frmAddNew.mobileNumber3Textfield.Text = MobilePhone3;
                    frmAddNew.homeNumber1Textfield.Text = HomePhone1;
                    frmAddNew.homeNumber2Textfield.Text = HomePhone2;
                    frmAddNew.homeNumber3Textfield.Text = HomePhone3;
                    frmAddNew.workNumber1Textfield.Text = WorkPhone1;
                    frmAddNew.workNumber2Textfield.Text = WorkPhone2;
                    frmAddNew.workNumber3Textfield.Text = WorkPhone3;
                    frmAddNew.faxNumber1Textfield.Text = FaxPhone1;
                    frmAddNew.faxNumber2Textfield.Text = FaxPhone2;
                    frmAddNew.faxNumber3Textfield.Text = FaxPhone3;
                    frmAddNew.email1Textfield.Text = Email1;
                    frmAddNew.email2Textfield.Text = Email2;
                    frmAddNew.email3Textfield.Text = Email3;

                    frmAddNew.ShowDialog();

                }
            }
        }
    }
}